<template>
  <div class="department">
    <div class="btnRow">
      <el-button type="primary" @click="add">新增科室</el-button>
    </div>
    <div class="record">
      <el-table :data="departmentList" style="width: 100%">
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="danger" @click="del(scope.row)">删除</el-link>
          </template>
        </el-table-column>

        <el-table-column prop="departmentId" label="科室id"></el-table-column>
        <el-table-column prop="departmentName" label="科室名字"></el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        align="right"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
        style="margin-top: 58px; margin-bottom: 40px"
      ></el-pagination>
    </div>
    <el-dialog title="新增科室" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="form1" label-width="80px">
        <el-form-item label="科室名字">
          <el-input v-model="departmentName" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="formConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    adminName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      departmentList: [],
      departmentName: "",
      pageIndex: 1,
      dialogVisible: false,
      pageSize: 10,
      total: 0
    };
  },
  created() {
    this.getDepartmentList();
  },
  methods: {
    getDepartmentList() {
      this.$axios.get("/api/CjwDepartment/GetDepartmentList").then(res => {
        // console.log(res);
        this.departmentList = res.data;
        console.log(this.departmentList);
      });
    },
    add() {
      this.departmentName = "";
      this.dialogVisible = true;
    },
    del(obj) {
      // console.log(obj);
      this.$confirm("此操作将删除选中的科室,是否确定?", "删除病床", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get(
              "/api/CjwDepartment/DelDepartment?departmentId=" +
                obj.departmentId
            )
            .then(res => {
              this.$message({
                type: "success",
                message: "删除科室成功!"
              });
              this.getDepartmentList();
              let op = {
                opAdminName: this.adminName,
                opTable: "删除科室",
                opData: `科室id:${obj.departmentId}科室名:${obj.departmentName}`
              };
              this.$axios.post("/api/CjwOperationList/InsertOpData", op);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getDepartmentList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getDepartmentList();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    formConfirm() {
      let params = {
        departmentName: this.departmentName
      };
      this.$axios.post("/api/CjwDepartment/AddDepartment", params).then(res => {
        this.$message({
          type: "success",
          message: "新增科室成功"
        });
        this.dialogVisible = false;
        this.getDepartmentList();
        let op = {
          opAdminName: this.adminName,
          opTable: "新增科室",
          opData: `科室名字:${params.departmentName}`
        };
        this.$axios.post("/api/CjwOperationList/InsertOpData", op);
      });
    }
  }
};
</script>

<style scoped>
.btnRow {
  margin: 10px;
}
</style>